<!DOCTYPE html>
<html>
<head>
  <link type="text/css" href="http://code.jquery.com/ui/1.8.16/themes/ui-lightness/jquery-ui.css" rel="Stylesheet" />
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script src="http://code.jquery.com/ui/1.8.16/jquery-ui.min.js"></script>
  <style>
     .ui-widget { font-size: 13px; }
     .score { list-style-type: none; 
              margin: 10px; 
              cursor: pointer; }
     .entryscore { font-size: 15px;
                   color: #0F0FF0; 
                   margin-left: 10px; }
     .entryusername { color: #F00000; }
     .entrydate { font-size: 11px;
                  color: #0F0F0F; }
     p { color: gray; }
  </style>
  <title>The DGOST: Diggr Global Online Score Table</title>
</head>

<body>

  <h4>The <a href="http://code.google.com/p/diggr-roguelike/">Diggr</a> Global Online Score Table</h4>

  <p>Welcome. <a href="http://code.google.com/p/diggr-roguelike/">Diggr</a> is a roguelike game.</p>
  <p>You can download it from the official site, <a href="http://code.google.com/p/diggr-roguelike/">http://code.google.com/p/diggr-roguelike/</a>. <br/> Play it to place an entry into this scoretable. Anybody can participate!</p>

  <p>See also: the official <a href="/wiki.py/FrontPage">Wiki</a>.</p>

  <p style="color: black;"><br/>Showing scores for version <span id="versiondisplay">12.02.26</span><br/>
  (See also: <a href="/oldversions.html">scoretables of previous versions</a>.)
  </p>

  <div style="margin-top: 30px; margin-left: 30px; width: 500px; border: 1px dashed #AFAFAF; padding: 10px;">

  <form>
    <div id="sortby">
	  <input type="radio" id="sortbytime" name="sortby"/>
      <label for="sortbytime">Sort by time</label>
	  <input type="radio" id="sortbyscore" name="sortby"/>
      <label for="sortbyscore">Sort by score</label>
    </div>
  </form>

  <h5>Scores:</h5>

  <ul id="entries"/>
  </ul>
  <button id="scrollleft">&#x2190;</button> Showing entries <span id="entries-from"></span>-<span id="entries-to"></span>
  <button id="scrollright">&#x2192;</button>

  <ul id="filters" style="list-style-type:none">
  <li><button id="filteradd">Add achievement filter</button>
  <button id="userfilter">Filter by username</button>
  </li>
  </ul>

  <button id="reload">Refresh</button>
  </div>

<script>

function getparams() {
   var a = document.location.search.slice(1).split("&");
   var ret = {};
   for (i in a) { 
     var tmp = a[i].split("=");
     ret[tmp[0]] = tmp[1];
   }
   return ret;
}


var GET = getparams();

window.offset = 0;
window.limit = 50;
window.default_version = "12.02.26";

function format(item) {
   var date = new Date(item["time"] * 1000);
   return '&#x27E9; <span class="entryscore">' + item["score"] + "</span> points, by " +
          '<span class="entryusername">' + item["username"] + "</span> " +
          '<span class="entrydate">(' + date.toGMTString() + ")</span>";
}

function showgame(id) {
  var params = { "gameid" : id };

  if (GET.version) { params["version"] = GET.version; }

  $.getJSON("scripts/global-highscore.py",
            params,
      function(data) {
        var date = new Date(data["time"] * 1000);

        var text = "Played by: " + data["username"] + "<br/>" +
                   "Time: " + date.toGMTString() + "<br/>" +
                   "Score: " + data["score"] + "<br/>" + 
                   "Placed #" + data["place"] + " out of " + data["numgames"] + "<br/>" +
                   "<br/>Achievements:<br/><br/>" +
                   '<table border="0">';

        var lis = [];
        $.each(data["achievements"], function(i,item) {
           lis.push("<tr><td>" + item["text"] + "</td><td>#" + item["place"] + " of " + item["numgames"] + "</td></tr>");
        });

        text += lis.join("");
        text += "</table>";

        var ver;
        if (GET.version) {
           ver = GET.version;
        } else {
           ver = window.default_version;
        }

        text += '<p><a href="/scripts/global-highscore.py?download=' + id + 
                "&version=" + ver + '">Download this game</a></p>';
        text += '<a href="game.html?id=' + id + "&version=" + ver + '">' +
                '<span style="font-size: 8px">Permalink</span></a>';

        $("<div/>").attr("title", "Game ID: " + id).html(text).dialog({width: "auto"});
     })
}

function reload() {

  var params = {};

  if (GET.version) { params["version"] = GET.version; }

  if (window.sortbytime == true) {
    params["sort"] = 2;
  } else if (window.sortbyscore == true) {
    params["sort"] = 1;
  }

  $("#entries").html("");

  params["ach"] = $.map($("select"), function(i) { return i.value; });

  params["offset"] = window.offset;
  params["limit"] = window.limit;

  $.getJSON("scripts/global-highscore.py",
    params,
    function(data) {
      $.each(data, function(i,item){
        $("<li/>").attr("id", "i" + i).attr("class", "score").
                   html(format(item)).click(
            function() { showgame(item["id"]); }).appendTo("#entries");
      });

      if (window.offset == 0) {
         $("#scrollleft").button("option", "disabled", true);
      } else {
         $("#scrollleft").button("option", "disabled", false);
      }

      if (data.length < window.limit) {
         $("#scrollright").button("option", "disabled", true);
      } else {
         $("#scrollright").button("option", "disabled", false);
      }

      $("#entries-from").html(window.offset+1);
      $("#entries-to").html(window.offset + data.length);
  });
}

window.filtersnum = 0;

function get_achievements() {

  var params = { "get_achievements" : 1 };

  if (GET.version) { params["version"] = GET.version; }

  $.getJSON("scripts/global-highscore.py",
            params,
      function(data) {
        var selc = $("<select/>").attr("id", "filterselect").change(reload);
        $.each(data["achievements"], function(i,item) {
           $("<option/>").attr("value", item["achievement"]).html(item["text"]).appendTo(selc);
        });

        var liid = "fli"+window.filtersnum;

        $("<li/>").attr("id", liid).html(selc).appendTo("#filters");
        $('<span style="cursor:pointer">[&#x2718;]</span>').click(function() {
           $("#"+liid).remove();
           reload();
        }).appendTo("#"+liid);

        window.filtersnum++;
      }
  );
}


function filter_user() {
  var params = { "get_achievements" : 1 };

  if (GET.version) { params["version"] = GET.version; }

  $.getJSON("scripts/global-highscore.py",
            params,
      function(data) {
        var selc = $("<select/>").attr("id", "userselect").change(reload);
        $.each(data["usernames"], function(i,item) {
           $("<option/>").attr("value", item["achievement"]).html(item["text"]).appendTo(selc);
        });

        var liid = "userfilteroption";

        $("<li/>").attr("id", liid).html(selc).appendTo("#filters");
        $('<span style="cursor:pointer">[&#x2718;]</span>').click(function() {
           $("#"+liid).remove();
           $("#userfilter").show();
           reload();
        }).appendTo("#"+liid);

	$("#userfilter").hide();
      }
  );
}


$("#sortbytime").click(function() {
  window.sortbytime = true;
  window.sortbyscore = false;
  reload();
});

$("#sortbyscore").click(function() {
  window.sortbytime = false;
  window.sortbyscore = true;
  reload();
});

$("#filteradd").click(get_achievements);
$("#userfilter").click(filter_user);

$("#reload").click(reload);

$("#scrollleft").click(function() {
  window.offset -= window.limit;
  reload();
});

$("#scrollright").click(function() {
  window.offset += window.limit;
  reload();
});

$("#sortby").buttonset();
$("#reload").button();
$("#filteradd").button();
$("#userfilter").button();
$("#scrollleft").button();
$("#scrollright").button();

if (GET.version) {
  $("#versiondisplay").html(GET.version);
}

reload();


</script>

</body>
</html>
